// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "canteen_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "食堂",
    "label": "食堂"
  },
  "food_type_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "菜品类",
    "label": "菜品类"
  },
  "name": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "菜品名称",
    "label": "菜品名称"
  },
  "icon_img": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "菜品图标",
    "label": "菜品图标"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "无法出售"
          },
          {
            "value": 1,
            "text": "可以出售"
          }
        ]
      }
    ],
    "title": "菜品的状态",
    "defaultValue": 1,
    "label": "菜品的状态"
  },
  "price": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "菜品价格",
    "label": "菜品价格"
  },
  "packaging_fee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "打包费",
    "label": "打包费"
  },
  "pelivery_fee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "配送费",
    "label": "配送费"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "monthly_sales": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "月售",
    "label": "月售"
  },
  "amount": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "份量",
    "label": "份量"
  },
  "single_point_delivery": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "单点是否配送",
    "defaultValue": true,
    "label": "单点是否配送"
  },
  "positive_reviews": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "好评度",
    "label": "好评度"
  }
}

const enumConverter = {
  "status_valuetotext": {
    "0": "无法出售",
    "1": "可以出售"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
